package com.u.social.article.controller;


import com.u.social.article.entity.Articles;
import com.u.social.article.model.ArticleWithTopicDTO;
import com.u.social.article.service.IArticleIndexService;
import com.u.social.article.service.IArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Alex Wu
 * @since 2025-01-23
 */
@RestController
@RequestMapping("/articles")
public class ArticlesController {

    @Autowired
    private IArticlesService articlesService;

    @Autowired
    private IArticleIndexService iArticlesService;

    @PostMapping("/")
    public String create(@RequestBody Articles article) {
        articlesService.save(article);
        return "Article created successfully";
    }


    @PostMapping("withTopic")
    public ArticleWithTopicDTO createWithTopic(@RequestBody ArticleWithTopicDTO articleWithTopicDTO) {
        ArticleWithTopicDTO dto = articlesService.saveWithTopic(articleWithTopicDTO);
        return dto;
    }

    @PostMapping("/async-withTopic")
    public Long asyncCreateWithTopic(@RequestBody ArticleWithTopicDTO articleWithTopicDTO) {
        return articlesService.asyncSaveArticle(articleWithTopicDTO);
    }

    @GetMapping("/public-result/{id}")
    public Boolean getPublicResultById(@PathVariable Long id) {
        return articlesService.loadPublicResult(id);
    }


    @PostMapping("/drafts")
    public ArticleWithTopicDTO createDrafts(@RequestBody ArticleWithTopicDTO articleWithTopicDTO) {
        articleWithTopicDTO.getArticle().setStatus(0);
        ArticleWithTopicDTO dto = articlesService.saveWithTopic(articleWithTopicDTO);
        return dto;
    }


    @GetMapping("/search/keyword/{keyword}")
    public List<String> getRecommendEntryByKeyword(@PathVariable  String keyword) {
        return iArticlesService.findRecommendEntryByKeyword(keyword);
    }

    /**
     * 根据ID获取文章信息
     *
     * @param id 文章的唯一标识符
     * @return 返回 Articles 对象，如果找不到则返回 null
     */
    @GetMapping("/{id}")
    public Articles getById(@PathVariable Long id) {
        return articlesService.getById(id);
    }

    @GetMapping("/hot-list/{start}/{end}")
    public List<Articles> getHotArticles(@PathVariable long start, @PathVariable long end) {
        return articlesService.findHotArticles(start, end);
    }


    @GetMapping("/mine/{userId}")
    public Articles getHomePageListByUserId(@PathVariable Long userId) {
        return articlesService.getById(userId);
    }

    @GetMapping("/top/list")
    public List<ArticleWithTopicDTO> getMineList() {
        return articlesService.getMineList(123L);
    }

    @GetMapping("/user/{userId}")
    public Articles getOtherList(@PathVariable Long userId) {
        return articlesService.getById(null);
    }

    @GetMapping("/recommend/user/{userId}")
    public Articles getRecommendByUserId(@PathVariable String userId) {
        return articlesService.getById(userId);
    }

    @PutMapping("/update")
    public Boolean update( @RequestBody ArticleWithTopicDTO articleWithTopicDTO) {
        return articlesService.updateWithTopic(articleWithTopicDTO);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable Long id) {
        Boolean deleted = articlesService.softDelete(id);
        return deleted;
    }

}
